import 'package:flutter/material.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/model/partner_count_data_model.dart';
import 'package:flutter_app_unionad/model/partner_dev_list_model.dart';
import 'package:flutter_app_unionad/request/partner_ship_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_widget.dart';
import 'package:flutter_app_unionad/ui/basice/my_separator_widget.dart';

class FacilityListPage extends StatefulWidget {
  static const String routeName = "/facilityListPage";

  @override
  _FacilityListPageState createState() => _FacilityListPageState();
}

class _FacilityListPageState extends State<FacilityListPage> {
  List<PartnerDevListModel>? dataSource;
  PartnerCountDataModel? dataModel;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    PartnerShipRequest.partnerCountDataRequest().then((value) {
      if (mounted)
        setState(() {
          dataModel = value;
        });
    });
    PartnerShipRequest.getPartnerDevList().then((value) {
      if (mounted)
        setState(() {
          dataSource = value;
        });
    });
  }

  @override
  Widget build(BuildContext context) {
    return BaseNavigationWidget(
      child: buildContent(),
      needScrollView: false,
      title: "设备列表",
    );
  }

  Widget buildContent() {
    if (dataModel == null) return ProgressDialog.showFirstLoading();
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5.px),
      ),
      padding: EdgeInsets.symmetric(vertical: 30.px),
      child: Column(
        children: [
          buildTitleWidget(),
          MySizeBox(height: 40),
          buildBottomWidget(),
          MySizeBox(height: 10),
          Expanded(
            child: ListView.builder(
              itemCount: dataSource == null ? 0 : dataSource!.length,
              padding: EdgeInsets.zero,
              itemBuilder: (ctx, index) {
                return buildListItem(index);
              },
            ),
          ),
          MySizeBox(height: 10),
        ],
      ),
    );
  }

  Widget buildListItem(int index) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 30.px, vertical: 5.px),
      child: Row(
        // mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            width: 100.px,
            child: Text(
              "设备:${dataSource![index].devId}",
              style: TextStyle(
                color: MyColors.color_27_27_27,
                fontSize: MyFont.font12,
                fontWeight: MyFontWeight.regular,
              ),
            ),
          ),
          SizedBox(width: 30.px),
          Container(
            width: 100.px,
            child: Text(
              "总收益： ${dataSource![index].totalIncome}",
              style: TextStyle(
                color: MyColors.color_27_27_27,
                fontSize: MyFont.font12,
                fontWeight: MyFontWeight.regular,
              ),
            ),
          ),
          Expanded(child: SizedBox()),
          Container(
            width: 60.px,
            alignment: Alignment.center,
            child: Text(
              dataSource![index].status == 2 ? "运行中" : "空闲",
              style: TextStyle(
                color: MyColors.color_27_27_27,
                fontSize: MyFont.font12,
                fontWeight: MyFontWeight.regular,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget buildBottomWidget() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 30.px),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '设备类别',
            style: TextStyle(
              color: MyColors.color_27_27_27,
              fontWeight: MyFontWeight.bold,
              fontSize: MyFont.font16,
            ),
          ),
          MySizeBox(height: 15),
          MySeparator(height: 2, color: MyColors.color_238_238_238),
        ],
      ),
    );
  }

  Widget buildTitleWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        buildTitleItem("总设备：", "${dataModel!.totalDevNum}"),
        buildTitleItem("空闲设备：", "${dataModel!.idleNum}"),
        buildTitleItem("运行设备：", "${dataModel!.runningNum}"),
      ],
    );
  }

  Widget buildTitleItem(String title, String detail) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text(
          title,
          style: TextStyle(
            color: MyColors.formHex("#272727ff"),
            fontSize: MyFont.font13,
            fontWeight: MyFontWeight.regular,
          ),
        ),
        Text(
          detail,
          style: TextStyle(
            color: MyColors.formHex("#272727ff"),
            fontSize: MyFont.font16,
            fontWeight: MyFontWeight.bold,
          ),
        )
      ],
    );
  }
}
